package buss.console.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.base.common.SysParam;
import com.base.pay.MySecureProtocolSocketFactory;

@SuppressWarnings("deprecation")
public class JdHttpUtil {

 private static Logger log = Logger.getLogger(JdHttpUtil.class);
 private static Protocol https = null;

 static {
  https = new Protocol("https", new MySecureProtocolSocketFactory(), 443);
  Protocol.registerProtocol("https", https);
 }
	
	public static DefaultHttpClient getHttpClient(String proxyIP, int port) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String proxyHost = proxyIP;
		int proxyPort = port;
		String userName = "******";
		String password =  SysParam.getSysParam("password");
		httpClient.getCredentialsProvider().setCredentials(
				new AuthScope(proxyHost, proxyPort),
				new UsernamePasswordCredentials(userName, password));
		HttpHost proxy = new HttpHost(proxyHost, proxyPort);
		httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,
				proxy);
		return httpClient;
	}
 
	public static String jdpostProxy (String url, String content){
		String retSrc = "";
		try {
			String proxyIP = SysParam.getSysParam("envProxyIP");
			String jdhost = SysParam.getSysParam("jdHost");

			HttpPost post = new HttpPost(url);
			HttpEntity se;
		
			se = new StringEntity(content,"UTF-8");
			post.setEntity(se);

			post.setHeader("Accept","*/*");
			post.setHeader("Referer","http://club.cpic.com.cn");
			post.setHeader("Accept-Language","zh-cn,en-us;q=0.5");
			post.setHeader("Content-Type","application/x-www-form-urlencoded");
			post.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; .NET CLR 1.0.3705; .NET CLR 1.1.4322)");
			post.setHeader("Host",jdhost);
			//post.setHeader("Content-Length","159");
			post.setHeader("Pragma","no-cache");
			post.setHeader("Cache-Control","no-cache");

			
			//启用代理
			HttpResponse httpResponse = getHttpClient(proxyIP,8002).execute(post);
			//不启用代理
			//HttpResponse httpResponse = new DefaultHttpClient().execute(post);
			retSrc = EntityUtils.toString(httpResponse.getEntity());
		} catch (UnsupportedEncodingException e) {
			log.error(e);
		} catch (ClientProtocolException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
		return retSrc;
	}
	
	
	/**
	  * 
	  * HttpClient发送json
	  * 
	  * @param url
	  * @param content
	  * @param charSet
	  * @return
	  */
	 public static String sendByPost(String url, String content, String charSet) {
		 String proxyIP = SysParam.getSysParam("envProxyIP");
		 log.info("接口请求路径：" + url);
		 HttpPost post = new HttpPost(url);
			HttpEntity se;
			String retSrc = "";
			try {
				se = new StringEntity(content);
//				se = new StringEntity(content, charSet);
				post.setEntity(se);
				//启用代理
				HttpResponse httpResponse = getHttpClient(proxyIP,8002).execute(post);
				//不启用代理
			//	HttpResponse httpResponse = new DefaultHttpClient().execute(post);
				retSrc = EntityUtils.toString(httpResponse.getEntity());
			} catch (UnsupportedEncodingException e) {
				log.error("接口请求异常1：" + e);
			} catch (ClientProtocolException e) {
				log.error("接口请求异常2：" + e);
			} catch (IOException e) {
				log.error("接口请求异常3：" + e);
			}
			log.info("接口返回结果：" + retSrc);
			return retSrc;
		}
}